package vn.com.pavibook.admin.controller;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeAdminController{
	private static final Logger logger = LoggerFactory.getLogger(HomeAdminController.class);
	@RequestMapping(value="/admin/home", method=RequestMethod.GET)
	public String openHomepage(Model model){
		logger.info("HOME PAGE");
		SecurityContext context = SecurityContextHolder.getContext();
		String username = context.getAuthentication().getName();
		Object user = context.getAuthentication().getPrincipal();
		String role = StringUtils.EMPTY;
		if(user instanceof User){
			User userDetail = (User)user;
			for (GrantedAuthority ga : userDetail.getAuthorities() ) {
				role = ga.getAuthority();
			}
		}
		logger.info(role);
		model.addAttribute("username", username);
		return "admin/home-page";
	}
}
